'''
This script is used to read config file and output cmake macro

to define macros used in cmake
    set(CFG_USE_16550 True)

to define macros used in source file
    add_definitions(-DCFG_USE_16550=True)
'''

def to_cmake_var(line):
    v = line.split('=')[-1].strip()
    k = line.split('=')[0].strip()
    if v == 'y': v = 'True'
    if v == 'yes': v = 'True'
    if v == 'no':  v = 'False'
    if v == 'n':  v = 'False'
    return  'set({0} {1})'.format(k, v)

def to_source_macro(line):
    return 'add_definitions(-D{})'.format(line)

def read_configs(file_name):
    f = open(file_name)
    lines = f.readlines()
    f.close()
    lines = map(lambda x:x.strip(), lines)
    lines = list(filter(lambda x: x.startswith('CFG'),lines))
    srcdef = map(to_source_macro, lines)
    cmkdef = map(to_cmake_var, lines)
    
    for line in srcdef: print(line)
    for line in cmkdef: print(line)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='generate config file in cmake style')
    # Add optional args and there definitions
    parser.add_argument('--cfg', type=str, default='defconfig', help='specify the config')
    args = parser.parse_args()
    read_configs(args.cfg)
    